import {
	getTaskProcessor2, // 上报用时用
	getTaskProcessor, // 工作人员权限,处理人选择时用
	getTaskHandler,
	getDutyDept,
	getDeptUsers
} from "@/api/Interface/event"
import {
	HandleMap
} from "@/enums/report"

/**
 * @description 工作人员用,获取下一节点处理人是否有权责部门
 * @param {String} bs 事件详情.data.eventSubclass 有值是2 否则为1
 * @param {String} eventTypeId
 */
export const useGetDutyDept = async (params) => {
	return new Promise(async (resolve, reject) => {
		await getDutyDept(params).then(res => {
			resolve(res)
		}).catch(err => {
			reject(err)
		})
	})
}

/**
 * @description 获取权责部门下人员
 * @param {String} deptId 部门id
 */
export const useGetDeptUsers = async (params) => {
	return new Promise(async (resolve, reject) => {
		try {
			const {
				code,
				rows
			} = await getDeptUsers(params)
			if (code === 200) {
				const list = rows;
				resolve({
					code: 200,
					data: useFormatDatas(list, {
						label: "nickName",
						value: "userId",
						taskType: HandleMap.user
					})
				})
			} else {
				resolve({
					code: 404,
					data: []
				})
			}
		} catch (e) {
			reject({
				code: 500,
				data: []
			})
		}
	})
}

/**
 * @description 获取事件处理类型(下一节点处理人选择级联组件父节点数据)
 * @param {String} taskId
 * @param {Number} lat
 * @param {Number} lon
 * @param {Number} type 0:事件上报时选择处理人,  1:工作人员处理事件时选择处理人
 */
export const useHandleType = async (params, type) => {
	return new Promise(async (resolve, reject) => {
		try {
			const Api = type === 0 ? getTaskProcessor2 : getTaskProcessor;
			await Api(params).then(res => {
				if (res.code === 200) {
					const {
						taskDealType = null, title = null, data = []
					} = res.data.taskData;
					resolve({
						code: 200,
						data: [{
							label: title,
							value: taskDealType,
							_taskType: taskDealType,
							source: data
						}]
					})
				} else {
					resolve({
						code: 404,
						data: []
					})
				}
			})
		} catch (e) {
			//TODO handle the exception
			reject({
				code: 500,
				data: []
			})
		}
	})
}

/**
 * @description 获取网格列表 (下一节点处理人选择级联组件子孙节点数据) 
 */
export const useGetEventGrid = async (list) => {
	return Promise.resolve(useFormatDatas(list, {
		label: "grid_name",
		value: "grid_id",
		taskType: HandleMap.grid
	}))
}

/**
 * @description 获取角色列表 (下一节点处理人选择级联组件子孙节点数据) 
 */
export const useGetEventRole = async (list) => {
	return Promise.resolve(useFormatDatas(list, {
		label: "roleName",
		value: "roleId",
		taskType: HandleMap.role
	}))
}

/**
 * @description 获取部门列表 (下一节点处理人选择级联组件子孙节点数据) 
 */
export const useGetEventDept = async (list) => {
	return Promise.resolve(useFormatDatas(list, {
		label: "deptName",
		value: "deptId",
		taskType: HandleMap.dept
	}))
}

/**
 * @description 获取人员列表 (下一节点处理人选择级联组件子孙节点数据) 
 */
export const useGetEventUser = async (list) => {
	return Promise.resolve(useFormatDatas(list, {
		label: "nick_name",
		value: "user_id",
		taskType: HandleMap.user
	}))
}

/**
 * @description 获取孙节点列表 (下一节点处理人选择级联组件子孙节点数据) 
 */
export const useGetEventGrandson = async (params) => {
	return new Promise(async (resolve, reject) => {
		try {
			const {
				code,
				rows: data
			} = await getTaskHandler(params)
			if (code === 200) {
				const list = data;
				resolve({
					code: 200,
					data: useFormatDatas(list, {
						label: "nick_name",
						value: "user_id",
						taskType: HandleMap.user
					})
				})
			} else {
				resolve({
					code: 404,
					data: []
				})
			}
		} catch (e) {
			//TODO handle the exception
			reject({
				code: 500,
				data: []
			})
		}
	})
}

// export const useGetEventGrid = async (params) => {
// 	return new Promise(async (resolve, reject) => {
// 		try {
// 			const {
// 				code,
// 				data
// 			} = await getTaskHandler(params)
// 			if (code === 200) {
// 				const list = data;
// 				resolve({
// 					code: 200,
// 					data: useFormatDatas(list, {
// 						label: "nick_name",
// 						value: "user_id"
// 					})
// 				})
// 			} else {
// 				resolve({
// 					code: 404,
// 					data: []
// 				})
// 			}
// 		} catch (e) {
// 			//TODO handle the exception
// 			reject({
// 				code: 500,
// 				data: []
// 			})
// 		}
// 	})
// }

export const useFormatDatas = (list, {
	label,
	value,
	taskType
}) => {
	if (!list || list.length === 0) return [];
	list.forEach(item => {
		item.label = item[label];
		item.value = item[value];
		item._taskType = taskType
	})
	return list;
}